//
//  DetailCommunityCell.m
//  shusheng
//
//  Created by rimi on 16/7/18.
//  Copyright © 2016年 yuying. All rights reserved.
//

#import "DetailCommunityCell.h"
#import "SDAutoLayout.h"

@implementation DetailCommunityCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {

        
       
        
        [self initializeUserInterface];
        NSLog(@"%lu",(unsigned long)self.data.count);
    }
    return self;
}



- (void)initializeUserInterface{
    
    
    [self.contentView addSubview:self.headImageView];
//    [self.headImageView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self).with.offset(10);
//        make.left.equalTo(self).with.offset(10);
//        make.width.mas_equalTo(0.1 * SCREEN_WIDTH);
//        make.height.mas_equalTo(0.1 * SCREEN_WIDTH);
//    }];
        self.headImageView.sd_layout
        .topSpaceToView(self.contentView,10)
        .leftSpaceToView(self.contentView,10)
        .widthIs(SCREEN_WIDTH *0.1)
    .heightIs(SCREEN_WIDTH *0.1);
    
    
    
    [self.contentView addSubview:self.nameLbael];
//    [self.nameLbael mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self).with.offset(10);
//        make.left.mas_equalTo(self.headImageView.mas_right).with.offset(5);
//        make.right.mas_equalTo(self.mas_centerX).with.offset(5);
//        make.height.mas_equalTo(0.1 * SCREEN_WIDTH /2);
//    }];
    self.nameLbael.sd_layout
    .topEqualToView(self.headImageView)
    .leftSpaceToView(self.headImageView,5)
    .rightSpaceToView(self.contentView,SCREEN_WIDTH/2 +5)
    .heightIs(0.1 * SCREEN_WIDTH /2);
    
    [self.contentView addSubview:self.timeLabel];
//    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(self.fnameLbael.mas_bottom).with.offset(0);
//        make.left.mas_equalTo(self.headImageView.mas_right).with.offset(5);
//        make.right.mas_equalTo(self.mas_centerX).with.offset(5);
//        make.height.mas_equalTo(0.1 * SCREEN_WIDTH /2);
//    }];
    self.timeLabel.sd_layout
    .topSpaceToView(self.nameLbael,0)
    .leftSpaceToView(self.headImageView,5)
    .rightSpaceToView(self.contentView,SCREEN_WIDTH/2 +5)
    .heightIs(0.1 * SCREEN_WIDTH /2);
    
    
    [self.contentView addSubview:self.contenLabel];
//    [self.contenLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(self.headImageView.mas_bottom).with.offset(5);
//        make.left.mas_equalTo(self.headImageView.mas_right).with.offset(5);
//        make.right.equalTo(self).with.offset(-10);
//        make.bottom.equalTo(self).with.offset(-10);
//    }];
      self.contenLabel.sd_layout
    .topSpaceToView(self.headImageView,5)
    .leftSpaceToView(self.headImageView,5)
    .rightSpaceToView(self.contentView,10);
    self.contenLabel.sd_layout.autoHeightRatio(0);
    
    
    [self.contentView addSubview:self.replyButton];
//    [self.replyButton mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self).with.offset(15);
//        make.right.equalTo(self).with.offset(-30);
//        make.width.mas_equalTo(40);
//        make.height.mas_equalTo(30);
//    }];
    self.replyButton.sd_layout
    .topSpaceToView(self.contentView,15)
    .rightSpaceToView(self.contentView,30)
    .widthIs(40)
    .heightIs(30);
    
    
    
    [self.contentView addSubview:self.pinlunView];
    self.pinlunView.sd_layout
    .topSpaceToView(self.contenLabel,-5)
    .leftSpaceToView(self.contentView,SCREEN_WIDTH *0.2)
    .rightSpaceToView(self.contentView,SCREEN_WIDTH *0.1)
    .bottomSpaceToView (self.contentView,5);
    
//    lastTopView = self.pinlunView;
//    [self addUnser];
//    for (int i = 0; i < _data.count; i++) {
//        UILabel *label = (UILabel *)self.commentLabelsArray[i];
//        label.hidden = NO;
//        
//        CGFloat topMargin = (i == 0) ? 10 : 5;
//        label.sd_layout
//        .leftEqualToView(self.contenLabel)
//        .rightEqualToView(self.contenLabel)
//        .topSpaceToView(lastTopView, topMargin)
//        .autoHeightRatio(0);
//        
//        label.isAttributedContent = YES;
//        lastTopView = label;
//    }
//    
    

//     [self setupAutoHeightWithBottomView:self.contenLabel bottomMargin:10];
    
    
}

-(void)setData:(NSArray *)data
{
    _data = data;
    //从父视图移除 数组清空
    for (UILabel *label in self.commentLabelsArray) {
        [label removeFromSuperview];
    }
    [self.commentLabelsArray removeAllObjects];
    
    [self addUnser];
    
    [self setupAutoHeightWithBottomView:self.lastTopView bottomMargin:10];
}

- (void)addUnser{
    self.lastTopView = self.contenLabel;

    for (int i = 0; i < _data.count; i ++) {
        UILabel *label = [[UILabel alloc] init];
        label.font = [UIFont systemFontOfSize:14];
//        label.backgroundColor = [UIColor redColor];
        
//        label.text = @"asadasadasad";
        NSString *nameStr = _data[i][@"commentUser"];
        NSString *secondUser = _data[i][@"secondUser"];
        NSUInteger sencond = secondUser.length;
        NSUInteger nameLong = nameStr.length;
//        NSString *contenString = _data[i][@"commentData"];
//        NSString *huifu = [contenString substringWithRange:NSMakeRange(nameLong,2)];
        
        NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:_data[i][@"commentData"]];
        
        [str addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0,nameLong)];
        
//        [str addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(19,6)];
        [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial-BoldItalicMT" size:15.0] range:NSMakeRange(0, nameLong)];
        
//        [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Courier-BoldOblique" size:30.0] range:NSMakeRange(19, 6)];
        if (secondUser) {
           [str addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(nameLong + 2,sencond) ];
            [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:15.0] range:NSMakeRange(nameLong +2, sencond)];
        }
        
        label.attributedText = str;
//        label.attributedText = _data[i][@"commentData"];
        label.userInteractionEnabled=YES;
        label.tag = i + 1000;
        [label addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTap:)]];
        
        [self.contentView addSubview:label];
        [self.commentLabelsArray addObject:label];
       
        CGFloat topMargin = (i == 0) ? 10 : 5;
        label.sd_layout
        .leftSpaceToView(self.contentView,SCREEN_WIDTH *0.2 + 10)
        .rightSpaceToView(self.contentView,SCREEN_WIDTH *0.1 + 10)
        .topSpaceToView(self.lastTopView, topMargin)
        .autoHeightRatio(0);
        
        label.isAttributedContent = YES;
        self.lastTopView = label;
    }

}

- (void)labelTap:(UITapGestureRecognizer *)recognizer{
     UILabel *labels = (UILabel*)recognizer.view;
    NSString *nameStr = _data[labels.tag - 1000][@"commentUser"];
    NSUInteger nameLong = nameStr.length;
    NSUInteger userLong = [AVUser currentUser].username.length;
    _detailVc.secondUser = nameStr;
    _detailVc.sendButton.tag = self.replyButton.tag;
    _detailVc.bottomView.hidden = NO;
    NSString *str = [[AVUser currentUser].username stringByAppendingFormat:@"回复%@：",nameStr];
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:str];
    [string addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0,userLong)];
    [string addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(userLong + 2,nameLong)];
//            [string addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(19,6)];
    [string addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial-BoldItalicMT" size:15.0] range:NSMakeRange(0, userLong)];
    [string addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:15.0] range:NSMakeRange(nameLong +2, nameLong)];
//            [string addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Courier-BoldOblique" size:30.0] range:NSMakeRange(19, 6)];
    _detailVc.textField.attributedText = string;
//        NSLog(@"%ld",(long)labels.tag);
    
    

}


- (NSMutableArray *)commentLabelsArray
{
    if (!_commentLabelsArray) {
        _commentLabelsArray = [[NSMutableArray alloc] init];
    }
    return _commentLabelsArray;
}






#pragma mark -- UITableViewDataSource && UITableViewDelegate

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [[UITableViewCell alloc] init];
    return cell;

}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 4;
}


#pragma mark -- Getters




- (UIImageView *)headImageView{
    if (!_headImageView) {
        _headImageView = [[UIImageView alloc] init];
        _headImageView.layer.cornerRadius = 3;
//        _headImageView.backgroundColor = [UIColor orangeColor];
        
    }
    return _headImageView;
}

- (UILabel *)nameLbael{
    if (!_nameLbael) {
        _nameLbael = [[UILabel alloc] init];
        
        [_nameLbael setTextColor:[UIColor blueColor]];
//        _nameLbael.backgroundColor = [UIColor orangeColor];
    }
    return _nameLbael;
}

- (UILabel *)timeLabel{
    if (!_timeLabel) {
        _timeLabel = [[UILabel alloc] init];
//        _timeLabel.backgroundColor = [UIColor lightGrayColor];
        _timeLabel.font = [UIFont systemFontOfSize:13];
       
    }
    return _timeLabel;
}


- (UILabel *)contenLabel{
    if (!_contenLabel) {
        _contenLabel = [[UILabel alloc] init];
//        _contenLabel.backgroundColor = [UIColor greenColor];
        _contenLabel.numberOfLines = 2;
        
    }
    return _contenLabel;
}


- (UIButton *)replyButton{
    if (!_replyButton) {
        _replyButton = [[UIButton alloc] init];
        [_replyButton setImage:[UIImage imageNamed:@"写回复 (1) (1).png"] forState:UIControlStateNormal];
        
        [_replyButton addTarget:self action:@selector(respondsToReplyButton:) forControlEvents:UIControlEventTouchUpInside];
//        _replyButton.backgroundColor = [UIColor purpleColor];
    }
    return _replyButton;
}

- (void)respondsToReplyButton:(UIButton *)sender{
   
    _detailVc.sendButton.tag = sender.tag;
    _detailVc.bottomView.hidden = NO;
    NSUInteger userLong = [AVUser currentUser].username.length;
    NSString *string = [[AVUser currentUser].username stringByAppendingString:@"："];
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:string];
    [str addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0,userLong)];
    [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial-BoldItalicMT" size:15.0] range:NSMakeRange(0, userLong)];
    _detailVc.textField.attributedText = str;
    NSLog(@"回复");
}

- (UIImageView *)pinlunView{
    if (!_pinlunView) {
        _pinlunView = [UIImageView new];
//        NSString *imageName = [NSString stringWithFormat:@"LikeCmtBg.png"];
//        UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForAuxiliaryExecutable:imageName]];
        UIImage *bgImage = [[[UIImage imageNamed:@"LikeCmtBg.png"] stretchableImageWithLeftCapWidth:100 topCapHeight:100] imageWithRenderingMode:UIImageRenderingModeAutomatic];
        
//        _pinlunView.backgroundColor = [UIColor colorWithPatternImage:bgImage];
        _pinlunView.backgroundColor = [UIColor clearColor];
        _pinlunView.image = bgImage;
        
    }
    return _pinlunView;
}

//- (NSArray *)data{
//    if (!_data) {
//        _data = [[NSArray alloc] initWithObjects:@"asasd",@"sadsad", nil];
//    }
//    return _data;
//}


@end

